package com.qp.smsmarketing.webservice;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

public class WebService {

//	GlobalController controller = new GlobalController();
	
	private String URL = "http://192.168.2.100:8082/cweb/ChamaGarcomWS";
	private String NAMESPACE = "http://webservice.qp.com.br/ChamaGarcomService";
	private String SERVER = "emais.sytes.net";
        private String PORT = "8080";
	
	public WebService(){
	}
	
	public Object consumirMetodo(String webservice,String metodo,
                List<Parametro> listParams)
	{
		
		setNAMESPACE("http://webservice.session.emais.quintaparte.com/"+webservice+"Service");
		setURL("http://"+SERVER+":"+PORT+"/emais/"+webservice+"WS");
		
		Object retorno = null;
		
		SoapObject request = new SoapObject(NAMESPACE, metodo);
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
		SoapEnvelope.VER11);
		envelope.setOutputSoapObject(request);
		
		request = prepararListaParametros(request,listParams);
		
		HttpTransportSE httpTransport = new HttpTransportSE(URL);
					
		try {
			
			httpTransport.call("",envelope);
			retorno = envelope.getResponse();
			
		} catch (IOException e) {
			retorno = null;
			e.printStackTrace();
		} catch (XmlPullParserException e) {
			retorno = null;
			e.printStackTrace();
		}
		return retorno;
	}
	
	private SoapObject prepararListaParametros(SoapObject request,List<Parametro> list){
		
		for(Parametro p : list){
			request.addProperty(p.getNome(),p.getValue());
		}
		return request;
	}
	
	
	public String getURL() {
		return URL;
	}
	public void setURL(String uRL) {
		URL = uRL;
	}
	public String getNAMESPACE() {
		return NAMESPACE;
	}
	public void setNAMESPACE(String nAMESPACE) {
		NAMESPACE = nAMESPACE;
	}
	
}
